//
//  YJJViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2016/12/30.
//  Copyright © 2016年 汪的一声就哭了！. All rights reserved.
//
//轮播高度
#define ScrollHeight WIDTH*197.0/720.0
#define NavHeight 64
#define TabbarHeight 49
//容器标题高度
#define TitleHeight self.fontSize*3

#import "YJJViewController.h"

@interface YJJViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) EmptyView *emptyView;
@property (nonatomic, strong) ReasonView *reasonView;
@end

@implementation YJJViewController
{
    NSMutableArray *_listArray;
    NSInteger page;
}
- (void)setState:(NSString *)state
{
    _state = state;
    [self obtainTaskDatawithhud:NO];
}
#pragma mark ---------- 懒加载原因
- (ReasonView *)reasonView
{
    if (!_reasonView) {
        _reasonView = [[NSBundle mainBundle] loadNibNamed:@"ReasonView" owner:self options:nil].firstObject;
        _reasonView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        [_reasonView.sureBtn addTarget:self action:@selector(deleteReasonAction:) forControlEvents:UIControlEventTouchUpInside];
        _reasonView.backView.layer.cornerRadius = 5;
        _reasonView.backView.layer.borderColor = [UIColor colorWithRed:217.0/255.0 green:218.0/255.0 blue:220.0/255.0 alpha:1].CGColor;
        _reasonView.backView.layer.borderWidth = 1;
    }
    return _reasonView;
}
- (void)deleteReasonAction:(UIButton *)btn
{
    [self.reasonView removeFromSuperview];
}
#pragma mark --------- 懒加载
- (EmptyView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[NSBundle mainBundle] loadNibNamed:@"EmptyView" owner:self options:nil].firstObject;
        _emptyView.frame = CGRectMake(0, 0, WIDTH, self.tableView.frame.size.height);
    }
    return _emptyView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BackColor;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 4, WIDTH, HEIGHT-ScrollHeight-NavHeight-TabbarHeight-TitleHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"JXZTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [self addRefresh];
    [self obtainTaskDatawithhud:NO];
}
- (void)addRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(obtain)];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JXZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    cell.backgroundColor = [UIColor clearColor];
    cell.stateLabel.text = @"已拒绝";
    cell.secBtn.tag = 1000+indexPath.row;
    [cell.firstBtn addTarget:self action:@selector(bianjiAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.secBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.thirdBtn addTarget:self action:@selector(fuzhiAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.thirdBtn.tag = 10000+indexPath.row;
    
    [cell.forthBtn setTitle:@"原因" forState:UIControlStateNormal];
    [cell.forthBtn addTarget:self action:@selector(yuanyinAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.forthBtn.tag = 100;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    Tasklist *task = _listArray[indexPath.row];
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:task.t_img] placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.nameLabel.text = task.t_name;
    cell.moneyLabel.text = [NSString stringWithFormat:@"%@元每单",task.money];
    cell.leftLabel.text = [NSString stringWithFormat:@"剩余%@个",task.num];
    cell.timeLabel.text = task.time;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate YJJViewControllerChooseYuLanWith:[NSString stringWithFormat:@"http://www.vmijob.com/userlook/index/tdetail/task_id/%@/index/b",[_listArray[indexPath.row] t_id]]];
}
#pragma mark ------------- 复制
- (void)fuzhiAction:(UIButton *)btn
{
    [[SQShowMessage shareMessage] showMessageWithStr:@"敬请期待."];
}
- (void)bianjiAction:(UIButton *)btn
{
    [[SQShowMessage shareMessage] showMessageWithStr:@"正在开发."];
}
#pragma mark ------------ 原因
- (void)yuanyinAction:(UIButton *)btn
{
    Tasklist *model = _listArray[btn.tag - 100];
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [app.window addSubview:self.reasonView];
    self.reasonView.yuanyinLabel.text = model.yuanyin;
    NSLog(@"%ld",model.yuanyin.length);
}
- (void)timerAction:(NSTimer *)timer
{
    [self.tableView numberOfRowsInSection:0];
}
- (void)obtain
{
    [self obtainTaskDatawithhud:NO];
}
- (void)deleteAction:(UIButton *)btn
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"reviewdeltask",@"id":[_listArray[btn.tag-1000] t_id]} HUD:YES block:^(id responseObject) {
        if ([responseObject[@"code"] isEqual:@200]) {
            
        } else {
            [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
        }
        [self obtainTaskDatawithhud:NO];
    }];
}
#pragma mark -------------- 已拒绝任务
- (void)obtainTaskDatawithhud:(BOOL)hud
{
    page = 1;
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"tasklist",@"uid":UID,@"type":@"2",@"page":[NSString stringWithFormat:@"%ld",page]} HUD:hud block:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            _listArray = [NSMutableArray array];
            for (NSDictionary *dic in responseObject[@"result"]) {
                Tasklist *model = [[Tasklist alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        if (_listArray.count == 0) {
            [self.view addSubview:self.emptyView];
        }else {
            [self.emptyView removeFromSuperview];
        }
    }];
}
- (void)loadMoreTopics
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"tasklist",@"uid":UID,@"type":@"2",@"page":[NSString stringWithFormat:@"%ld",page]} HUD:NO block:^(id responseObject) {
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            for (NSDictionary *dic in responseObject[@"result"]) {
                Tasklist *model = [[Tasklist alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
